﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bierdopje.API
{
    /// <summary>
    /// This is an representation of the Show structure returned by the
    /// bierdopje API
    /// </summary>
    public class ApiShow
    {
        private int m_ShowID;

        public int ShowID
        {
            get { return m_ShowID; }
            set { m_ShowID = value; }
        }
        private int m_TVDBID;

        public int TVDBID
        {
            get { return m_TVDBID; }
            set { m_TVDBID = value; }
        }
        private string m_Name;

        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        private string m_Link;

        public string Link
        {
            get { return m_Link; }
            set { m_Link = value; }
        }
        private DateTime m_FirstAired;

        public DateTime FirstAired
        {
            get { return m_FirstAired; }
            set { m_FirstAired = value; }
        }
        private DateTime m_LastAired;

        public DateTime LastAired
        {
            get { return m_LastAired; }
            set { m_LastAired = value; }
        }
        private DateTime m_NextEpisode;

        public DateTime NextEpisode
        {
            get { return m_NextEpisode; }
            set { m_NextEpisode = value; }
        }
        private int m_Seasons;

        public int Seasons
        {
            get { return m_Seasons; }
            set { m_Seasons = value; }
        }
        private int m_Episodes;

        public int Episodes
        {
            get { return m_Episodes; }
            set { m_Episodes = value; }
        }
        private string m_ShowStatus;

        public string ShowStatus
        {
            get { return m_ShowStatus; }
            set { m_ShowStatus = value; }
        }
        private string m_Network;

        public string Network
        {
            get { return m_Network; }
            set { m_Network = value; }
        }
        private string m_AirTime;

        public string AirTime
        {
            get { return m_AirTime; }
            set { m_AirTime = value; }
        }
        private int m_RunTime;

        public int RunTime
        {
            get { return m_RunTime; }
            set { m_RunTime = value; }
        }
        private Decimal m_Score;

        public Decimal Score
        {
            get { return m_Score; }
            set { m_Score = value; }
        }
        private int m_Favorites;

        public int Favorites
        {
            get { return m_Favorites; }
            set { m_Favorites = value; }
        }
        private bool m_Translators;

        public bool Translators
        {
            get { return m_Translators; }
            set { m_Translators = value; }
        }
        private string m_Summary;

        public string Summary
        {
            get { return m_Summary; }
            set { m_Summary = value; }
        }

        /// <summary>
        /// Get all episodes for this show
        /// </summary>
        /// <returns>A list of episodes</returns>
        public List<ApiEpisode> GetAllEpisodes()
        {
            return m_ApiLibrary.GetAllEpisodesForShow(this.ShowID);
        }

        /// <summary>
        /// Get all episodes for a specific season for this show
        /// </summary>
        /// <param name="season">The season to retrieve episodes for</param>
        /// <returns>A list of episodes</returns>
        public List<ApiEpisode> GetAllEpisodesForSeason(int season)
        {
            return m_ApiLibrary.GetEpisodesForSeason(this.ShowID, season);
        }

        /// <summary>
        /// Hide the default constructor
        /// </summary>
        private ApiShow()
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Make sure the object is only created by the API, need for refrence issues
        /// </summary>
        /// <param name="apiLibrary">Reference to the parent library</param>
        internal ApiShow(ApiLibrary apiLibrary)
        {
            m_ApiLibrary = apiLibrary;
        }

        private ApiLibrary m_ApiLibrary;

        /// <summary>
        /// Fetches a collection of episodes for a list of seasons
        /// </summary>
        /// <param name="seasons">A list of seasons List of int's</param>
        /// <returns>A list of ApiEpisodes</returns>
        public List<ApiEpisode> GetAllEpisodesForSeason(List<int> seasons)
        {
            // Check if we can retrieve the episodes in 1 call
            if (seasons.Count == this.Seasons)
            {
                return this.GetAllEpisodes();
            }
            else
            {
                // we have a subset of seasons, create a summary list to put all eps in
                List<ApiEpisode> _FullEpisodeList = new List<ApiEpisode>();

                foreach (int season in seasons)
                {
                    // retrieve this seasons episodes an load the in the list
                    List<ApiEpisode> _TempEpisodeList = this.GetAllEpisodesForSeason(season);
                    foreach (ApiEpisode _Ep in _TempEpisodeList)
                    {
                        _FullEpisodeList.Add(_Ep);
                    }
                }
                return _FullEpisodeList;
            }
        }

        /// <summary>
        /// Gets a specific episode
        /// </summary>
        /// <param name="season">Season</param>
        /// <param name="episode">Episode</param>
        /// <returns>The episode or null if ep isn't found</returns>
        public ApiEpisode GetEpisode(int season, int episode)
        {
            // Get all episodes
            List<ApiEpisode> _Episodes = this.GetAllEpisodesForSeason(season);

            // Find correct episode
            foreach (ApiEpisode _Ep in _Episodes)
            {
                if (_Ep.Episode == episode)
                    return _Ep;
            }

            // Episode not found!
            return null;
        }
    }
}
